import {computed} from "vue"
import Activity from "/src/views/crm/componentsNew/ActivityNew/index.vue";
import CRMEditBaseInfo from "@/views/crm/componentsNew/BaseInfo/CRMEditBaseInfo.vue";
import RelativeContacts from "@/views/crm/componentsNew/relative/RelativeContacts.vue";
import RelativeTeam from "@/views/crm/componentsNew/relative/RelativeTeam.vue";
import RelativeBusiness from "@/views/crm/componentsNew/relative/RelativeBusiness.vue";
import RelativeContract from "@/views/crm/componentsNew/relative/RelativeContract.vue";
import RelativeReturnMoney from "@/views/crm/componentsNew/relative/RelativeReturnMoney.vue";
import RelativeVisit from "@/views/crm/componentsNew/relative/RelativeVisit.vue";
import RelativeInvoice from "@/views/crm/componentsNew/relative/RelativeInvoice.vue";
import RelativeFiles from "@/views/crm/componentsNew/relative/RelativeFiles.vue";
import RelativeHandle from "@/views/crm/componentsNew/relative/RelativeHandle.vue";
import {crmLeadsNumAPI} from "@/api/crm/leads.js";
import {crmCustomerNumAPI} from "@/api/crm/customer.js";
import {crmContactsNumAPI} from "@/api/crm/contacts.js";
import {crmBusinessNumAPI} from "@/api/crm/business.js";
import {crmContractNumAPI} from "@/api/crm/contract.js";
import {crmProductNumAPI} from "@/api/crm/product.js";
import {crmReceivablesNumAPI} from "@/api/crm/receivables.js";
import {ref} from "vue"
import {useErpUserStore} from "@/store/modules/usererp.js";

// todo whf tabNames 是各个应用自己特有的，需要在各个模块中自定义
export function useSzTab(crmOption, customerId) {

  // 从store中拿到crm权限
  const crm = computed(() => {
    const erpUserStore = useErpUserStore();
    return erpUserStore.crm
  })

  // 当前tab名称
  const currentTabName = ref('Activity')
  const tabsNumber = ref({})

  const showTabsNumber = computed(() => {
    return crmOption.crmType !== 'visit'
  })


  /**
   * tabs列表
   */
  const tabNames = computed(() => {
    let tempsTabs = []
    tempsTabs.push({label: '活动', name: 'Activity', comp: Activity})
    if (crm.value.customer && crm.value.customer.read) {
      tempsTabs.push({label: '详细资料', name: 'CRMEditBaseInfo', comp: CRMEditBaseInfo})
    }
    if (crm.value.contacts && crm.value.contacts.index) {
      console.log("tabsNumber", tabsNumber)
      tempsTabs.push({
        label: getTabName('联系人', tabsNumber.value.contactCount),
        name: 'RelativeContacts',
        comp: RelativeContacts
      })
    }

    tempsTabs.push({label: getTabName('团队成员', tabsNumber.value.memberCount), name: 'RelativeTeam', comp: RelativeTeam})

    if (crm.value.business && crm.value.business.index) {
      tempsTabs.push({
        label: getTabName('商机', tabsNumber.value.businessCount),
        name: 'RelativeBusiness',
        comp: RelativeBusiness
      })
    }

    if (crm.value.contract && crm.value.contract.index) {
      tempsTabs.push({
        label: getTabName('合同', tabsNumber.contractCount),
        name: 'RelativeContract',
        comp: RelativeContract
      })
    }
    if ((crm.value.receivables && crm.value.receivables.index) || (crm.value.receivablesPlan && crm.value.receivablesPlan.index)) {
      tempsTabs.push({
        label: getTabName('回款', tabsNumber.value.receivablesCount),
        name: 'RelativeReturnMoney',
        comp: RelativeReturnMoney
      })
    }
    if (crm.value.visit && crm.value.visit.index) {
      tempsTabs.push({
        label: getTabName('回访', tabsNumber.value.returnVisitCount),
        name: 'RelativeVisit',
        comp: RelativeVisit
      })
    }

    if (crm.value.invoice && crm.value.invoice.index) {
      tempsTabs.push({
        label: getTabName('发票', tabsNumber.value.invoiceCount),
        name: 'RelativeInvoice',
        comp: RelativeInvoice
      })
    }

    tempsTabs.push({label: getTabName('附件', tabsNumber.value.fileCount), name: 'RelativeFiles', comp: RelativeFiles})
    tempsTabs.push({label: '操作记录', name: 'RelativeHandle', comp: RelativeHandle})
    return tempsTabs
  })

  /**
   * 获取tab数量
   */
  const getTabsNum = ()=> {
    if (!showTabsNumber.value) {
      return
    }
    const request = {
      leads: crmLeadsNumAPI,
      customer: crmCustomerNumAPI,
      contacts: crmContactsNumAPI,
      business: crmBusinessNumAPI,
      contract: crmContractNumAPI,
      product: crmProductNumAPI,
      receivables: crmReceivablesNumAPI
    }[crmOption.crmType]

    if (!request) {
      return
    }

    console.log("aaaaaaaaaaaaaaaaaaaaa", customerId)
    const params = {}
    params[`id`] = customerId.value
    console.log("aaaaaaaaaaaaaaaaaaaaa", params)
    request(params).then(res => {
      console.log("aaaaaaaaaaaaaaaaaaaaa", res)
      tabsNumber.value = res.data.data || {}
    }).catch(() => {
    })
  }

  /**
   * 获取tabs名字
   * @param {*} name
   * @param {*} num
   */
  const getTabName = (name, num) => {
    return `${name}${num && num > 0 ? '（' + num + '）' : ''}`
  }


  return {currentTabName, tabsNumber, tabNames, getTabsNum, getTabName}
}